using System;
using System.Collections.Generic;
using System.Text;

namespace ChEngine
{
	partial class NegamaxEngine
	{
       
        int RecordHash(BitBoardDriver b,int depth, int val, TranspositionTableEntry.ValueType type)
        {
            //ulong key = b.ZobristKey(s);
            int index = (int)(b.ZKey % TranspositionTableSize);
            if (type == TranspositionTableEntry.ValueType.StaticEvaluation && transpositionTable[index].valType != TranspositionTableEntry.ValueType.None)
                return index;
            transpositionTable[index].zKey = b.ZKey;
            transpositionTable[index].valType = type;
            transpositionTable[index].value = val;
            transpositionTable[index].depth = depth;
            return index;
        }
        
        
        void RecordPV(int index, BitMove bm)
        {
            transpositionTable[index].move = bm;
        }
        void RecordPV( BitBoardDriver b, BitMove bm)
        {
            //ulong key = b.ZobristKey(s);
            int index = (int)(b.ZKey % TranspositionTableSize);
            transpositionTable[index].move = bm;
            transpositionTable[index].zKey = b.ZKey;
        }
        internal bool ProbePV(BitBoardDriver b,   out BitMove move)
        {
            //ulong key = b.ZobristKey(s);
            int index = (int)(b.ZKey % TranspositionTableSize);
            move = BitMove.NullMove;
            TranspositionTableEntry te = transpositionTable[index];
            if (te.zKey == b.ZKey && null != te.move && te.move.Current != 0 )
            {
                move = te.move;
                return true;
            }
            return false;
        }
        /*
        internal bool ProbeStaticEval( BitBoard b, out int w)
        {
            w = 0;
            //ulong key = b.ZobristKey(s);
            int index = (int)(b.ZKey % TranspositionTableSize);
            TranspositionTableEntry te = transpositionTable[index];
            if ( te.zKey == b.ZKey && te.valType != TranspositionTableEntry.ValueType.None
                 && te.valType != TranspositionTableEntry.ValueType.Beta 
                )
            {
                w = te.value;
                return true;
            }
            return false;
        }*/
        internal int GetTTIndex( BitBoardDriver b)
        {
            //ulong key = b.ZobristKey(s);
            return (int)(b.ZKey % TranspositionTableSize);
        }
        internal bool ProbeHash(BitBoardDriver b,int depth, ref int alpha, ref int beta,out int val)
        {
            //ulong key = b.ZobristKey(s);
            int index = (int)(b.ZKey % TranspositionTableSize);
            val = 0;
           
                TranspositionTableEntry te = transpositionTable[index];
                if (te.zKey == b.ZKey)
                {
                    if (te.depth >= depth)
                    {
                        if (te.valType == TranspositionTableEntry.ValueType.Exact)
                        {
                            val = te.value;
                            return true;
                        }
                        if (te.valType == TranspositionTableEntry.ValueType.Alpha && te.value > alpha)
                        {
                            alpha = te.value;
                        }
                        else
                            if (te.valType == TranspositionTableEntry.ValueType.Beta && te.value < beta)
                                beta = te.value;
                        if (alpha >= beta)
                        {
                            val = te.value;
                            return true;
                        }
                    }
                }
            
            return false;
        }
	}
}
